package com.itheima.reggie.config;

import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.common.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLIntegrityConstraintViolationException;

//异常处理
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler(SQLIntegrityConstraintViolationException.class)
    public R handleSQLConstraintException(SQLIntegrityConstraintViolationException exception) {
        String message = exception.getMessage();
        String[] s = message.split(" ");
        String replace = s[2].replace("'", "");
        //异常判断
        if (message.contains("Duplicate entry")) ;
        return R.error("用户名" + replace + "重复");
    }

    @ExceptionHandler(CustomException.class)
    public R exceptionHandler(CustomException ex){
        log.info(ex.getMessage());
        return R.error(ex.getMessage());
    }


}
